package com.leicx.guli.order.config;

import com.leicx.guli.common.constant.OrderMqConstant;
import com.leicx.guli.common.constant.WareMqConstant;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;

import java.util.HashMap;
import java.util.Map;


/**
 * @author daxiong
 * @date 2022/8/15 下午5:12
 */
@SpringBootConfiguration
public class MyOrderMqConfig {

    @Bean
    public Queue orderDelayQueue() {
        // String name, boolean durable, boolean exclusive, boolean autoDelete, Map<String, Object> arguments
        Map<String, Object> args = new HashMap<>();
        args.put("x-dead-letter-exchange", OrderMqConstant.EXCHANGE_ORDER_EVENT);
        args.put("x-dead-letter-routing-key", OrderMqConstant.ROUTE_KEY_ORDER_RELEASE_ORDER);
        args.put("x-message-ttl", 120000);
        return new Queue(OrderMqConstant.QUEUE_ORDER_DELAY, true, false, false, args);
    }

    @Bean
    public Queue orderReleaseOrderQueue() {
        return new Queue(OrderMqConstant.QUEUE_ORDER_RELEASE, true, false, false);
    }

    @Bean
    public Exchange orderEventExchange() {
        // String name, boolean durable, boolean autoDelete, Map<String, Object> arguments
        return new TopicExchange(OrderMqConstant.EXCHANGE_ORDER_EVENT, true, false);
    }

    @Bean
    public Binding orderCreateOrderBinding() {
        // String destination, DestinationType destinationType, String exchange, String routingKey, Map<String, Object> arguments
        return new Binding(OrderMqConstant.QUEUE_ORDER_DELAY, Binding.DestinationType.QUEUE, OrderMqConstant.EXCHANGE_ORDER_EVENT,
                OrderMqConstant.ROUTE_KEY_ORDER_CREATE, null);
    }

    @Bean
    public Binding orderReleaseOrderBinding() {
        return new Binding(OrderMqConstant.QUEUE_ORDER_RELEASE, Binding.DestinationType.QUEUE, OrderMqConstant.EXCHANGE_ORDER_EVENT,
                OrderMqConstant.ROUTE_KEY_ORDER_RELEASE_ORDER, null);
    }

    /**
     * 订单释放直接和库存释放进行绑定
     *
     * @param
     * @return org.springframework.amqp.core.Binding
     * @author daxiong
     * @date 2022/8/16 下午5:18
     */
    @Bean
    public Binding orderReleaseOtherBinding() {
        return new Binding(WareMqConstant.QUEUE_STOCK_RELEASE_STOCK, Binding.DestinationType.QUEUE, OrderMqConstant.EXCHANGE_ORDER_EVENT,
                "order.release.other.#", null);
    }

    /**
     * 商品秒杀队列
     *
     * @return
     */
    @Bean
    public Queue orderSecKillOrderQueue() {
        return new Queue(OrderMqConstant.QUEUE_ORDER_SECKILL, true, false, false);
    }

    @Bean
    public Binding orderSecKillOrderQueueBinding() {
        //String destination, DestinationType destinationType, String exchange, String routingKey,
        return new Binding(
                OrderMqConstant.QUEUE_ORDER_SECKILL,
                Binding.DestinationType.QUEUE,
                OrderMqConstant.EXCHANGE_ORDER_EVENT,
                "order.seckill.order",
                null);
    }

}
